package com.zlcx.tz.live.common.config;

import com.aliyun.oss.OSSClient;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.zlcx.tz.live.common.interceptor.RepeatSubmitInterceptor;
import com.zlcx.tz.live.common.interceptor.SignInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>webmvc配置</p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 * @author zl
 * @date Created in 2017/11/30 15:41
 * @version 1.0
 * @copyright: Copyright (c) founders
 */
@Slf4j
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

    @Autowired
    private SignInterceptor signInterceptor;

    @Value("${spring.profiles.active}")
    private String profile;

    @Value("${aliyun.oss.endpoint}")
    private String ossEndpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String ossAccessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String ossAccessKeySecret;

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;


    @Bean
    ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        return objectMapper;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(signInterceptor).addPathPatterns("/**");
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

//    @Override
//    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
//        configurer.setTaskExecutor(mvcAsyncExecutor());
//    }




    public static class AbortPolicy implements RejectedExecutionHandler {
        /**
         * Creates an {@code AbortPolicy}.
         */
        public AbortPolicy() { }

        /**
         * Always throws RejectedExecutionException.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         * @throws RejectedExecutionException always
         */
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            log.error("任务队列已满，拒绝接受新任务 Runnable = {} ThreadPoolExecutor = {}", r.toString(), e.toString());
        }
    }


    @Bean
    public OSSClient ossClient() {
        return new OSSClient(ossEndpoint, ossAccessKeyId, ossAccessKeySecret);
    }



    public static final long timeout = 5 * 1000;

    public enum WebTimeOutEnum {
        DOWN_ASSERTS_LIABILITY("/assets-liabilit/images", 10 * 1000),
        ;

        private String serviceUrl;
        private long timeout;

        WebTimeOutEnum(String serviceUrl, long timeout) {
            this.serviceUrl = serviceUrl;
            this.timeout = timeout;
        }

        public static WebTimeOutEnum url(String url) {
            for (WebTimeOutEnum timeOutEnum : WebTimeOutEnum.values()) {
                if (timeOutEnum.getServiceUrl().equals(url)) {
                    return timeOutEnum;
                }
            }
            return null;
        }

        public String getServiceUrl() {
            return serviceUrl;
        }

        public long getTimeout() {
            return timeout;
        }
    }
}
